package com.demo.datastructure.binarySearch;

/**
 * @description: 查找最后一个小于等于给定值的元素
 * @author: ljr
 * @time: 2021/5/14 12:48
 */
public class BinarySearch9 {

    public static int bsearch(int[] a, int n, int value) {
        int low = 0;
        int high = n - 1;
        while (low <= high){
            int mid = low +(high-low >> 1);
            if(a[mid] > value){
                high = mid -1;
            }else{
                if(mid == n-1 || a[mid+1] > value) return mid;
                low = mid+1;
            }
        }
        return  -1;
    }
}
